//
//  MYGHelp.swift
//  MYDoctor
//
//  Created by Denny on 2024/5/7.
//

import UIKit

class MYGHelp: NSObject {
    class func getChooseResult(dict:Dictionary<String, String>,action: @escaping (String,String)->()){
        let keys = dict.keys.sorted { (key1, key2) -> Bool in
            return key1 < key2
        }
        var values : [String] = []
        for key in keys {
            values.append(dict[key]!)
        }
        let s = NSDictionary(dictionary:dict)
        let picker = LZPicker.init(frame: UIScreen.main.bounds, dataArray: NSMutableArray(array:values))
        picker.dataArray = NSMutableArray(array:values)
        picker.selectedRow = "0"
        picker.back = { date in
            DispatchQueue.main.async {
                let ss = (s.filter({ (key,value) -> Bool in
                    if (value as! String) == date {
                        return true
                    }
                    return false
                })).last?.key
                if ss != nil {
                    action(ss as! String,date )
                } else {
                    action("",date )

                }
            }
        }
    }
    //7天后的日期
    class func after7Days()->String {
        let currentDate = Date()
        let calendar = Calendar.current
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        var result = ""
        if let sevenDaysLater = calendar.date(byAdding: .day, value: 7, to: currentDate) {
            result = dateFormatter.string(from: sevenDaysLater)
        }
        return result
    }
    
    //一个月后的日期
    class func after1Month()->String {
        let currentDate = Date()
        let calendar = Calendar.current
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        var result = ""
        if let oneMonthLater = calendar.date(byAdding: .month, value: 1, to: currentDate) {
            result = dateFormatter.string(from: oneMonthLater)
        }
        return result
    }
    
    //三个月后的日期
    class func after3Month()->String {
        let currentDate = Date()
        let calendar = Calendar.current
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        var result = ""
        if let threeMonthsLater = calendar.date(byAdding: .month, value: 3, to: currentDate) {
            result = dateFormatter.string(from: threeMonthsLater)
        }

        return result
    }
    
    //一年后的日期
    class func after1Year()->String {
        let currentDate = Date()
        let calendar = Calendar.current
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        var result = ""
        if let oneYearLater = calendar.date(byAdding: .year, value: 1, to: currentDate) {
            result = dateFormatter.string(from: oneYearLater)
        }
        return result
    }
    
    class func getCurrentTime(_ dateFormat:String = "yyyy-MM-dd")->String{
        let dateFormate = DateFormatter()
        dateFormate.dateFormat = dateFormat
        dateFormate.timeZone = TimeZone(identifier: "Asia/Shanghai")
        dateFormate.locale = Locale(identifier: "zh_CN")
        return dateFormate.string(from: Date())
    }
    //计算剩余天数
    class func daysUntil(_ date: Date) -> Int {
        let calendar = Calendar.current
        let now = Date()
        let components = calendar.dateComponents([.day], from: now, to: date)
        return components.day ?? 0
    }
}
